package com.jugheadzhou.auth.controller;

import com.jugheadzhou.auth.core.common.api.Result;
import com.jugheadzhou.auth.dto.LoginDto;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * vue admin template 初始接口接入
 *
 * @author jugheadzhou
 * @date 2021-11-26
 */
@RestController
@RequestMapping("vue-admin-template")
@CrossOrigin
public class VueAdminTemplateController {

    @PostMapping("/user/login")
    public Result<?> userLogin(@Valid @RequestBody LoginDto loginDto) {
        Map<String, Object> data = new HashMap<>();
        data.put("token", "admin-token");
        return Result.success(data);
    }

    @GetMapping("/user/info")
    public Result<?> userLogin(String token) {
        Map<String, Object> data = new HashMap<>();
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        data.put("introduction", "I am a super administrator 哈哈");
        data.put("name", "Super Admin");
        data.put("roles", Collections.singletonList("admin"));
        return Result.success(data);
    }

    @GetMapping("/user/logout")
    public Result<?> logout() {
        return Result.success("success");
    }

    @GetMapping("/table/list")
    public Result<?> list() {
        List<Map<String, Object>> items = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("id", i);
            item.put("title", "test title for " + i);
            item.put("status", i % 2 == 0 ? "是" : "否");
            item.put("author", "admin");
            item.put("display_time", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            item.put("pageviews", RandomUtils.nextInt());
            items.add(item);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("total", items.size());
        data.put("items", items);
        return Result.success(data);
    }
}
